#if NETFX_CORE
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.Windows.Input;
using Windows.UI.Xaml.Input;
#else
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
#endif

namespace FreshMeat.Xaml
{
   [TemplateVisualState(Name = "MouseOver", GroupName = "CommonStates")]
   [TemplateVisualState(Name = "Focused", GroupName = "FocusStates")]
   [TemplateVisualState(Name = "Unfocused", GroupName = "FocusStates")]
   [TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
   [TemplateVisualState(Name = "Unselected", GroupName = "SelectionStates")]
   [TemplateVisualState(Name = "Selected", GroupName = "SelectionStates")]
   [TemplateVisualState(Name = "SelectedUnfocused", GroupName = "SelectionStates")]
   public class DropMenuItem : ListBoxItem
   {
#if !SILVERLIGHT && !NETFX_CORE
      static DropMenuItem()
      {
         DefaultStyleKeyProperty.OverrideMetadata(typeof(DropMenuItem), new FrameworkPropertyMetadata(typeof(DropMenuItem)));
      }
#endif

      public DropMenuItem()
      {
         DefaultStyleKey = typeof(DropMenuItem);
      }

      public DropMenu Owner { get; internal set; }

      public readonly static DependencyProperty CommandProperty = DependencyProperty.Register(
         "Command",
         typeof(ICommand),
         typeof(DropMenuItem),
         new PropertyMetadata(null, (o, e) => ((DropMenuItem)o).CommandChanged((ICommand)e.OldValue, (ICommand)e.NewValue)));

      public readonly static DependencyProperty CommandParameterProperty = DependencyProperty.Register(
         "CommandParameter",
         typeof(object),
         typeof(DropMenuItem),
         new PropertyMetadata(null));

      public ICommand Command
      {
         get { return (ICommand)GetValue(CommandProperty); }
         set { SetValue(CommandProperty, value); }
      }

      public object CommandParameter
      {
         get { return GetValue(CommandParameterProperty); }
         set { SetValue(CommandParameterProperty, value); }
      }

      void CommandChanged(ICommand oldValue, ICommand newValue)
      {
         if (oldValue != null)
            oldValue.CanExecuteChanged -= CommandCanExecuteChanged;

         if (newValue != null)
            newValue.CanExecuteChanged += CommandCanExecuteChanged;

         UpdateIsEnabled();
      }

      void CommandCanExecuteChanged(object sender, EventArgs e)
      {
         UpdateIsEnabled();
      }

      void UpdateIsEnabled()
      {
         IsEnabled = Command != null && Command.CanExecute(CommandParameter);
      }

#if NETFX_CORE
      protected override void OnPointerPressed(PointerRoutedEventArgs e)
#else
      protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
#endif
      {
         e.Handled = true;

         if (Owner != null)
            Owner.ItemMouseDown(this);
         
#if NETFX_CORE
         base.OnPointerPressed(e);
#else
         base.OnMouseLeftButtonDown(e);
#endif
      }

#if NETFX_CORE
      protected override void OnPointerReleased(PointerRoutedEventArgs e)
#else
      protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
#endif
      {
         e.Handled = true;

         if (Owner != null)
            Owner.ItemMouseUp(this);
         
#if NETFX_CORE
         base.OnPointerReleased(e);
#else
         base.OnMouseLeftButtonUp(e);
#endif
      }

#if NETFX_CORE
      protected override void OnPointerEntered(PointerRoutedEventArgs e)
#else
      protected override void OnMouseEnter(MouseEventArgs e)
#endif
      {
#if !SILVERLIGHT
         e.Handled = true;
#endif

         if (Owner != null)
            Owner.ItemMouseEnter(this);

#if NETFX_CORE
         base.OnPointerEntered(e);
#else
         base.OnMouseEnter(e);
#endif
      }

#if NETFX_CORE
      protected override void OnPointerExited(PointerRoutedEventArgs e)
#else
      protected override void OnMouseLeave(MouseEventArgs e)
#endif
      {
#if !SILVERLIGHT
         e.Handled = true;
#endif

         if (Owner != null)
            Owner.ItemMouseLeave(this);

#if NETFX_CORE
         base.OnPointerExited(e);
#else
         base.OnMouseLeave(e);
#endif
      }

#if SILVERLIGHT || NETFX_CORE
      protected override void OnGotFocus(RoutedEventArgs e)
      {
         if (Owner != null)
            Owner.ItemGotFocus(this);

         base.OnGotFocus(e);
      }
#else
      protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
      {
         e.Handled = true;

         if (Owner != null)
            Owner.ItemGotFocus(this);

         base.OnGotKeyboardFocus(e);
      }
#endif

#if SILVERLIGHT || NETFX_CORE
      protected override void OnLostFocus(RoutedEventArgs e)
      {
         if (Owner != null)
            Owner.ItemLostFocus(this);

         base.OnLostFocus(e);
      }
#else
      protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
      {
         e.Handled = true;

         if (Owner != null)
            Owner.ItemLostFocus(this);
         
         base.OnLostKeyboardFocus(e);
      }
#endif
   }
}